<?php
    // 这个借口用于处理更新文章功能

    include_once '../../fn.php';

    // echo 'hehe';

    // echo '<pre>';
    // print_r($_POST);
    // echo '</pre>';

    // echo '<pre>';
    // print_r($_FILES);
    // echo '</pre>';

    // 获取基本数据
    $title = $_POST['title'];
    $content = $_POST['content'];
    $slug = $_POST['slug'];
    $category = $_POST['category'];
    $created = $_POST['created'];
    $status = $_POST['status'];
    $id = $_POST['id'];
    // 获取文件数据
    $feature = '';

    if ($_FILES['feature']['error']===0) {
        $firstName = explode('.',$_FILES['feature']['name'])[0];
        $lastName = explode('.',$_FILES['feature']['name'])[1];
        $newName = date(time()) . $firstName . '.' . $lastName;

        $feature = 'uploads/' . $newName;

        move_uploaded_file($_FILES['feature']['tmp_name'], '../../' . $feature);
    }

    // 构建数据库执行串
    // 基于$feature的值，决定是否更新图片存储的位置，因此要准备两个串
    if (empty($feature)) {
        // 不更新图片
        $sql = "update posts set slug = '$slug',title = '$title',created = '$created',content = '$content',status = '$status',category_id = '$category' where id = $id;";
    }else {
        // 更新图片
        $sql = "update posts set slug = '$slug',title = '$title',feature = '$feature'  ,created = '$created',content = '$content',status = '$status',category_id = '$category' where id = $id;";
    }

    $result = my_exec($sql);

    echo json_encode($result);
    
?>